package com.fenghuajueli.libbasecoreui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fenghuajueli.libbasecoreui.R;

/**
 * 通用控件，典型设置界面每一条选项那种，如左边有icon、跟着标题 或者右侧有箭头
 */
public class SuperLineBotton extends RelativeLayout {
    protected View contentView;
    protected Context mContext;
    private Integer leftIcon = -1;
    private String title = "";
    private Boolean showBottomLine = false;
    private String rightTips = "";
    private Boolean showRightTips = false;
    private Boolean showRightEnterIcon = false;
    private Boolean titleIsBold = false;
    private Integer icon = -1;
    private TextView tvTitle;
    private ImageView ivIcon;
    private TextView tvRightTips;
    private ImageView ivRightIcon;
    private View splitLine;
    private Integer rightIcon = -1;
    private int titleColor = Color.parseColor("#ffffff");

    public SuperLineBotton(Context context) {
        this(context, null);
    }

    public SuperLineBotton(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SuperLineBotton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SuperLineBotton);
        title = array.getString(R.styleable.SuperLineBotton_title);
        leftIcon = array.getResourceId(R.styleable.SuperLineBotton_lefticon, -1);
        showBottomLine = array.getBoolean(R.styleable.SuperLineBotton_showBottomLine, false);
        rightTips = array.getString(R.styleable.SuperLineBotton_rightTips);
        showRightEnterIcon = array.getBoolean(R.styleable.SuperLineBotton_showRightEnterIcon, false);
        showRightTips = array.getBoolean(R.styleable.SuperLineBotton_showRightTips, false);
        titleIsBold = array.getBoolean(R.styleable.SuperLineBotton_titleIsBold, false);
        rightIcon = array.getResourceId(R.styleable.SuperLineBotton_sl_rightIcon, R.mipmap.btn_inter);
        titleColor = array.getColor(R.styleable.SuperLineBotton_sl_titleColor, Color.parseColor("#ffffff"));
        array.recycle();
        mContext = context;
        initView();
    }

    private void initView() {
        contentView = RelativeLayout.inflate(mContext, R.layout.super_line_button_layout, this);
        tvTitle = contentView.findViewById(R.id.tvTitle);
        ivIcon = contentView.findViewById(R.id.ivIcon);
        splitLine = contentView.findViewById(R.id.splitLine);
        tvRightTips = contentView.findViewById(R.id.tvTips);
        ivRightIcon = contentView.findViewById(R.id.ivEnter);
        tvTitle.setTextColor(titleColor);
        if (leftIcon > 0) {
            ivIcon.setImageResource(leftIcon);
        }
        if (titleIsBold) {
            tvTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        }
        tvTitle.setText(title);
        if (showBottomLine) {
            splitLine.setVisibility(VISIBLE);
        } else {
            splitLine.setVisibility(GONE);
        }
        if (showRightTips) {
            tvRightTips.setVisibility(VISIBLE);
            tvRightTips.setText(rightTips != null ? rightTips : "");
        } else {
            tvRightTips.setVisibility(GONE);
        }
        ivRightIcon.setImageResource(rightIcon);
        if (showRightEnterIcon) {
            ivRightIcon.setVisibility(VISIBLE);

        } else {
            ivRightIcon.setVisibility(GONE);
        }
    }


    public void setRightTips(String tips) {
        this.rightTips = tips;
        tvRightTips.setText(tips);
    }
}
